﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace DengShijieShySharkWebApp.BM
{
	public partial class AddFl : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strConnection = "Data Source=LAPTOP-ANARUPVH;Initial Catalog=ARPDatabase;Persist Security Info=True;User ID=sa;Password=123456;Pooling=False;Encrypt=True;TrustServerCertificate=True";
            SqlConnection conn = new SqlConnection(strConnection);
            conn.Open();
            string selectSql = "SELECT FItNo FROM dtFlDetails";
            SqlCommand cmd = new SqlCommand(selectSql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "FlightNo");
            conn.Close();
            foreach (DataRow row in dataSet.Tables["FlightNo"].Rows)
            {
                
                if (row[0].ToString().Trim() == txtFlightNumber.Text.Trim())
                {
                    lblMessage.Text = "The Flight already exists. Please try another fligh number";
                    return;
                }
            }

            TimeSpan detime, arrtime;
            String DepDateTime, ArrDateTime;
            try
            {
                detime = Convert.ToDateTime(txtDepartureTime.Text.Trim()).TimeOfDay;
                arrtime = Convert.ToDateTime(txtArrivelTime.Text.Trim()).TimeOfDay;
                DepDateTime = Calendar1.SelectedDate.ToShortDateString() + " " + detime.ToString();
                ArrDateTime = Calendar2.SelectedDate.ToShortDateString() + " " + arrtime.ToString();
                if (detime >= arrtime)
                {
                    lblMessage.Text = "Departure Time can be greater than or equal to arrival time";
                    return;
                }
            }
            catch
            {
                lblMessage.Text = "Invalid departure or arrival time";
                return;
            }

           
            try
            {
                conn.Open();
                string updateSql = "INSERT INTO [dtFlDetails] ([FltNo], [Origin], [Destination], [Deptime], [Arrtime], [AircraftType], " +
                                  "[SeatsExec], [SeatsBN], [FareEx], [FareB], [LaunchDate]) " +
                                  "VALUES (@FltNo, @Origin, @Destination, @Deptime, @Arrtime, @AircraftType, " +
                                  "@SeatsExec, @SeatsBn, @FareExec, @FareBn, @LaunchDate)";
                SqlCommand cmd2 = new SqlCommand(updateSql, conn);
                cmd2.Parameters.AddWithValue("@FltNo", txtFlightNumber.Text.Trim());
                cmd2.Parameters.AddWithValue("@Origin", txtOrigin.Text.Trim());
                cmd2.Parameters.AddWithValue("@Destination", txtDestination.Text.Trim());
                cmd2.Parameters.AddWithValue("@Deptime", DepDateTime);
                cmd2.Parameters.AddWithValue("@Arrtime", ArrDateTime);
                cmd2.Parameters.AddWithValue("@AircraftType", txtAircraftType.Text.Trim());
                cmd2.Parameters.AddWithValue("@SeatsExec", Convert.ToInt32(txtNoOfExecSeats.Text.Trim()));
                cmd2.Parameters.AddWithValue("@SeatsBn", Convert.ToInt32(txtNoOfBussSeats.Text.Trim()));
                cmd2.Parameters.AddWithValue("@FareExec", Convert.ToInt32(txtExecFare.Text.Trim()));
                cmd2.Parameters.AddWithValue("@FareBn", Convert.ToInt32(txtBussFare.Text.Trim()));
                cmd2.Parameters.AddWithValue("@LaunchDate", DateTime.Today.Date.ToShortDateString());
                int n = cmd2.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                conn.Close();
                return;
            }
            conn.Close();
            lblMessage.Text = "Flight add successfully";
            txtFlightNumber.Text = "";
            txtOrigin.Text = "";
            txtAircraftType.Text = "";
            txtArrivelTime.Text = "";
            txtBussFare.Text = "";
            txtDepartureTime.Text = "";
            txtDestination.Text = "";
            txtExecFare.Text = "";
            txtFlightNumber.Text = "";
            txtNoOfBussSeats.Text = "";
            txtNoOfExecSeats.Text = "";
            txtOrigin.Text = "";
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            txtAircraftType.Text = "";
            txtArrivelTime.Text = "";
            txtBussFare.Text = "";
            txtDepartureTime.Text = "";
            txtDestination.Text = "";
            txtExecFare.Text = "";
            txtFlightNumber.Text = "";
            txtNoOfBussSeats.Text = "";
            txtNoOfExecSeats.Text = "";
            txtOrigin.Text = "";
        }
    }
}